package com.ktjy.test;

import com.ktjy.config.DaoConfig;
import com.ktjy.config.DataSourceConfig;
import com.ktjy.config.JavaConfig;
import com.ktjy.entity.SysUser;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.service.SysUserService;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Main {
    @Test
    public void test1(){
        //1.创建spring上下文对象
        ApplicationContext context=
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //2.获取我们想要对象
        SysUserMapper sysUserMapper = (SysUserMapper)context.getBean("sysUserMapper");

        SysUser sysUser=new SysUser();
        sysUser.setRealName("李");
        sysUser.setRoleId(2l);
        List<SysUser> sysUserList =
                sysUserMapper.getSysUserList(sysUser);

        for (SysUser user : sysUserList) {
            System.out.println(user);
        }
    }

    @Test
    public void test2(){
        //1.创建spring上下文对象
        ApplicationContext context=
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //2.获取我们想要对象
        SysUserService sysUserService = (SysUserService)context.getBean("sysUserService");

        SysUser sysUser=new SysUser();
        sysUser.setRealName("李");
        sysUser.setRoleId(2l);
        List<SysUser> sysUserList =
                sysUserService.getSysUserList(sysUser);

        for (SysUser user : sysUserList) {
            System.out.println(user);
        }
    }

    @Test
    public void test3(){
        //1.创建spring上下文对象
        ApplicationContext context=
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //2.获取我们想要对象
        SysUserService sysUserService = (SysUserService)context.getBean("sysUserService");

        SysUser sysUser=new SysUser();
        sysUser.setRealName("测试用户1");
        sysUser.setAccount("ceshi1");

        SysUser sysUser2=new SysUser();
        sysUser2.setRealName("测试用户2");
        sysUser2.setAccount("ceshi2");

        List<SysUser> list=new ArrayList<>();
        list.add(sysUser);
        list.add(sysUser2);

        sysUserService.saveBatch(list);
    }

    @Test
    public void test4(){
        //1.创建spring上下文对象
        ApplicationContext context=
                new AnnotationConfigApplicationContext(JavaConfig.class);
        //2.获取我们想要对象
        SysUser sysUser = (SysUser)context.getBean("sysUser");

        System.out.println(sysUser.getRealName());
        System.out.println(sysUser.getAccount());
        System.out.println(sysUser.getRole().getRoleName());
    }

    @Test
    public void test5(){
        //1.创建spring上下文对象
        ApplicationContext context=
                new AnnotationConfigApplicationContext(DataSourceConfig.class);
        //2.获取我们想要对象
        DataSourceConfig dataSourceConfig = (DataSourceConfig)context.getBean("dataSourceConfig");

        BasicDataSource basicDataSource=dataSourceConfig.dataSource();

    }
}
